package com.cn.wanxi.util;

import sun.misc.BASE64Encoder;

import java.math.BigDecimal;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Arrays;

public class Tool {
    public static Integer strToInt(String value) {
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    public static String nullToStr(String value) {
        try {
            return value == null ? "" : value;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }


    public static Integer strToPage(String value) {
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {
            e.printStackTrace();
            return 1;
        }
    }

    /**
     * 如果传递过来的参数不正确，则默认显10条数据
     *
     * @param value
     * @return
     */
    public static Integer strToLimit(String value) {
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {
            e.printStackTrace();
            return 10;
        }
    }

    public static String birthday(String value) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            simpleDateFormat.parse(value);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            return "1970-01-01";
        }
    }

    public static String encoderByMd5(String str) {
        //确定计算方法
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            BASE64Encoder base64en = new BASE64Encoder();
            //加密后的字符串
            return base64en.encode(md5.digest(str.getBytes("utf-8")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "C056Dl/oStNftflbnO6seQ==";
    }

    public static BigDecimal stringToBigDecimal(String value) {
        try {
            return new BigDecimal(value);
        } catch (Exception e) {
            e.printStackTrace();
            return BigDecimal.valueOf(-1);
        }
    }
}
